import Vue from 'vue'
import vuex from 'vuex'

Vue.use(vuex)
const initRouterTabs = new Array(0).fill({
  path: '/driveCabin',
  name: 'orderDispense',
  meta: {
    title: '异常订单'
  }
})
const state = {
  routerTabs: [...initRouterTabs],
  pageHeight: 0,
  formTypes: []
}

const mutations = {
  // 获取网页可见区域
  getViewPageHeight (state) {
    state.pageHeight = (document.documentElement.clientHeight || document.body.clientHeight) + 80
  },
  // 设置单据类型列表
  SET_FORM_TYPES (state, data) {
    state.formTypes = data
  },
  /**
   * 关闭所有路由
   * @param state
   * @constructor
   */
  CLOSE_ALL_ROUTER (state) {
    state.routerTabs = []
  },

  /**
   * 关闭其他路由
   * @param state
   * @param route
   * @constructor
   */
  CLOSE_OTHER_ROUTER (state, route) {
    state.routerTabs = state.routerTabs.filter(item => item.path === route.path)
  },

  /**
   * 添加路由
   * @param route
   * @constructor
   */
  ROUTER_TABS_PUSH (state, route) {
    const isHas = state.routerTabs.find(item => item.path === route.path)
    if (isHas) return
    state.routerTabs = [...state.routerTabs, route]
  },
  /**
   * 关闭单个路由
   * @param state
   * @param route
   * @constructor
   */
  CLOSE_ONE_ROUTER (state, route) {
    state.routerTabs = state.routerTabs.filter(item => item.path !== route.path)
  }
}

const actions = {
  // 设置供应商分类数据
  setFormTypes ({ commit }, data) {
    commit('SET_FORM_TYPES', data)
  },
  /**
   * 关闭全部路由
   * @param commit
   * @param route
   */
  closeAllRoute ({ commit }) {
    commit('CLOSE_ALL_ROUTER')
  },
  getViewPageHeight ({ commit }) {
    commit('getViewPageHeight')
  },
  /**
   * 添加一个router元素
   * @param commit
   */
  routerTabsPush ({ commit }, route) {
    commit('ROUTER_TABS_PUSH', route)
  },
  /**
   * 关闭其他路由
   * @param commit
   * @param route
   */
  closeOtherRoute ({ commit }, route) {
    commit('CLOSE_OTHER_ROUTER', route)
  },
  /**
   * 关闭单个路由
   * @param commit
   */
  closeOneRoute ({ commit }, route) {
    commit('CLOSE_ONE_ROUTER', route)
  }
}

export default new vuex.Store({
  state,
  actions,
  mutations
})
